package xmlexporter;

import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import shortcuthelper.Exporter;
import entry.Entry;

public class XmlExporter implements Exporter {

	@Override
	public void export(List<Entry> list, File target) throws IOException {
		createFileIfNotExists(target);
		serializeObjectToXML(target, list);
	}

	private void createFileIfNotExists(File target) throws IOException {
		if (!target.exists()) {
			target.createNewFile();
		}
	}

	private void serializeObjectToXML(File xmlFile, Object objectToSerialize)
			throws FileNotFoundException {
		FileOutputStream os = new FileOutputStream(xmlFile);
		XMLEncoder encoder = new XMLEncoder(os);
		encoder.writeObject(objectToSerialize);
		encoder.close();
	}

}
